"""
日程管理数据库模型
"""

from datetime import datetime
from enum import Enum as PyEnum

from sqlalchemy import Boolean, DateTime, Enum, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column

from src.database import Base


class ScheduleStatus(PyEnum):
    """日程状态枚举"""

    PENDING = "pending"  # 待处理
    IN_PROGRESS = "in_progress"  # 进行中
    COMPLETED = "completed"  # 已完成
    CANCELLED = "cancelled"  # 已取消


class SchedulePriority(PyEnum):
    """日程优先级枚举"""

    LOW = "low"  # 低优先级
    MEDIUM = "medium"  # 中优先级
    HIGH = "high"  # 高优先级
    URGENT = "urgent"  # 紧急


class ScheduleRepeatType(PyEnum):
    """日程重复类型枚举"""

    NONE = "none"  # 不重复
    DAILY = "daily"  # 每日重复
    WEEKLY = "weekly"  # 每周重复
    MONTHLY = "monthly"  # 每月重复
    YEARLY = "yearly"  # 每年重复


class Schedule(Base):
    """日程模型"""

    __tablename__ = "schedules"

    # 基本信息
    family_id: Mapped[int] = mapped_column(
        Integer, nullable=False, comment="家庭ID,用于区分不同家庭"
    )
    family_name: Mapped[str] = mapped_column(
        String(50), nullable=False, comment="家庭名称"
    )
    title: Mapped[str] = mapped_column(String(200), nullable=False, comment="日程标题")
    description: Mapped[str | None] = mapped_column(
        Text, nullable=True, comment="日程描述"
    )

    # 时间信息
    start_time: Mapped[datetime] = mapped_column(
        DateTime, nullable=False, comment="开始时间"
    )
    end_time: Mapped[datetime] = mapped_column(
        DateTime, nullable=False, comment="结束时间"
    )

    # 日程属性
    status: Mapped[ScheduleStatus] = mapped_column(
        Enum(ScheduleStatus),
        nullable=False,
        default=ScheduleStatus.PENDING,
        comment="日程状态",
    )
    priority: Mapped[SchedulePriority] = mapped_column(
        Enum(SchedulePriority),
        nullable=False,
        default=SchedulePriority.MEDIUM,
        comment="优先级",
    )

    # 重复设置
    repeat_type: Mapped[ScheduleRepeatType] = mapped_column(
        Enum(ScheduleRepeatType),
        nullable=False,
        default=ScheduleRepeatType.NONE,
        comment="重复类型",
    )
    repeat_until: Mapped[datetime | None] = mapped_column(
        DateTime, nullable=True, comment="重复截止日期"
    )

    # 提醒设置
    is_reminder_enabled: Mapped[bool] = mapped_column(
        Boolean, nullable=False, default=False, comment="是否启用提醒"
    )
    reminder_minutes: Mapped[int | None] = mapped_column(
        Integer, nullable=True, comment="提前提醒分钟数"
    )

    is_public: Mapped[bool] = mapped_column(
        Boolean, nullable=False, default=True, comment="是否是家庭日程"
    )

    def __repr__(self):
        return f"<Schedule(id={self.id}, title='{self.title}', start_time={self.start_time}, family_id={self.family_id})>"


class ScheduleParticipant(Base):
    """日程参与者模型"""

    __tablename__ = "schedule_participants"

    # 基本信息
    id: Mapped[int] = mapped_column(Integer, primary_key=True, comment="参与者ID")
    family_id: Mapped[int] = mapped_column(
        Integer, nullable=False, comment="家庭ID,用于区分不同家庭"
    )
    family_name: Mapped[str] = mapped_column(
        String(50), nullable=False, comment="家庭名称"
    )
    schedule_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="日程ID")
    member_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="成员ID")
    member_name: Mapped[str] = mapped_column(
        String(100), nullable=False, comment="成员名称"
    )

    # 参与状态
    is_required: Mapped[bool] = mapped_column(
        Boolean, nullable=False, default=True, comment="是否必需参与"
    )
    has_confirmed: Mapped[bool] = mapped_column(
        Boolean, nullable=False, default=False, comment="是否已确认参与"
    )
    confirmed_at: Mapped[datetime | None] = mapped_column(
        DateTime, nullable=True, comment="确认时间"
    )
    notes: Mapped[str | None] = mapped_column(
        Text, nullable=True, comment="参与备注"
    )
